﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebSiteAlex
{
    public partial class ExpenseReportControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //btnDelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure?')== false) return false;");
        }

        protected void OnLinqDataSourceSelecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            var query = DAL.ExpenseReport.LoadSearchResult(this.editSearchName.Text, 
                this.editSearchState.SelectedValue);
            e.Result = query;
        }

        private void SetEditMode(bool mode)
        {
            this.editPanel.Visible = mode;
            this.listPanel.Visible = !mode;
        }

        public override void DataBind()
        {
            this.editSearchState.DataSource = DAL.ExpenseReport.GetAllStates();
            this.editSearchState.DataBind();
            this.editSearchState.Items.Insert(0, string.Empty);
        }



        #region EventHandlers
        protected void OnSearch(object sender, EventArgs e)
        {
            this.grid.DataBind();
        }

        protected void OnNew(object sender, EventArgs e)
        {
            this.controlExpenseReport.ExpRepId = null;
            this.SetEditMode(true);
        }

        protected void OnSelected(object sender, EventArgs e)
        {
            Guid id = (Guid)grid.DataKeys[grid.SelectedIndex].Value;
            this.ViewState["PK"] = id;

            //TODO: Set the edit control Id here...
            this.controlExpenseReport.ExpRepId = id;
            this.controlExpenseReport.DataBind();

            this.SetEditMode(true);

        }

        protected void OnSave(object sender, EventArgs e)
        {
            //TODO: Call Edit control Save()
            this.controlExpenseReport.Save();
            
            this.SetEditMode(false);
            this.DataBind();
        }

        protected void OnReturn(object sender, EventArgs e)
        {
            this.SetEditMode(false);
        }

        protected void OnDelete(object sender, EventArgs e)
        {
            ImageButton imageButton = (ImageButton)sender;
            Guid itemId = new Guid(imageButton.CommandArgument);

            DAL.ExpenseReport.Delete(itemId);
            this.DataBind();
        }

        protected void OnSubmit(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            Guid itemId = new Guid(button.CommandArgument);

            DAL.ExpenseReport.Submit(itemId);
            this.grid.DataBind();
        }

        #endregion

    }
}